#include "Particle.h";

#ifndef __Common_h__
  #include "Common.h"
#endif //__Common_h__

static float g_particleID = 1.0f;

Particle::Particle(vec3 position) : 
	m_nID(g_particleID++),
	m_position(position), 
	m_oldPosition(position),
	m_acceleration(vec3(0.0f, 0.0f, 0.0f)), 
	m_nMass(1.0f), 
	m_bMovable(true), 
	m_accumulatedNormal(vec3(0.0f, 0.0f, 0.0f)),
	m_bFirstTimeUpdFromTransFeed(false)
{}

Particle::Particle()
{}

void Particle::addForce(vec3 f)
{
	m_acceleration += f/m_nMass;
}

void Particle::timeStep(float damping)
{
	if(m_bMovable)
	{
		vec3 temp = m_position;
		m_position = m_position + (m_position-m_oldPosition)*(1.0f-damping) + m_acceleration * TIME_STEPSIZE_POWER_2;
		m_oldPosition = temp;
	}
	m_acceleration = vec3(0.0f, 0.0f, 0.0f); // Acceleration is reset since it has been translated into a change in position (and implicitely into velocity)	
}

float Particle::getID()
{
	return m_nID;
}

vec3& Particle::getPosition() 
{
	return m_position;
}

void Particle::setPosition(vec3& p)
{
	m_position = p;
}

vec3& Particle::getOldPosition()
{
	return m_oldPosition;
}

void Particle::setOldPosition(vec3& p)
{
	m_oldPosition = p;
}

vec3& Particle::getAcceleration()
{
	return m_acceleration;
}

void Particle::setAcceleration(vec3& v)
{
	m_acceleration = vec3(v);
}

void Particle::resetAcceleration() 
{
	m_acceleration = vec3(0.0f, 0.0f, 0.0f);
}

void Particle::offsetPosition(const vec3 v) 
{ 
	if(m_bMovable) m_position += v;
}

void Particle::makeUnmovable() 
{
	m_bMovable = false;
}

void Particle::addToNormal(vec3 normal)
{
	m_accumulatedNormal += normalize(normal);
}

vec3& Particle::getNormal() 
{ 
	return m_accumulatedNormal;
} 

void Particle::resetNormal() 
{
	m_accumulatedNormal = vec3(0.0f, 0.0f, 0.0f);
}

void Particle::resetFirstTimeUpdFromTransFeed()
{
	m_bFirstTimeUpdFromTransFeed = false;
}

bool Particle::isUpdatedFromTransFeed()
{
	return m_bFirstTimeUpdFromTransFeed;
}

void Particle::setFirstTimeUpdFromTransFeed(bool val)
{
	m_bFirstTimeUpdFromTransFeed = val;
}